<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 标题组件 -->
        <TREETOOLS :trees="companyInfo" @on-delete="loadDapartment" @on-add="addDate" />
        <!-- 树形组件 -->
        <el-tree :data="treeData">
          <TREETOOLS slot-scope="{data}" :trees="data" @on-add="addDate" />
        </el-tree>
      </el-card>
      <!-- 部门添加组件 -->
      <DEPTSDIALOG :node="node" :dialogvisible.sync="dialogVisible" />
    </div>
  </div>
</template>

<script>
// 添加部门组件
import DEPTSDIALOG from './components/depts-dialog.vue'
// 树形结构组件
import TREETOOLS from './components/tree-tools.vue'
// 将数组数据转化成树形结构方法
import { listToTree } from '@/utils/index'
// 查询企业的部门列表请求
import { getDapartment } from '@/api/departments'
export default {
  name: 'Departments',
  components: {
    TREETOOLS,
    DEPTSDIALOG
  },
  data() {
    return {
      // 组件数据
      treeData: [],
      // 组件属性
      defaultProps: {},
      // 树形标题数据
      companyInfo: {
        name: '',
        manager: '负责人',
        root: true
      },
      // 控制部门添加对话框
      dialogVisible: false,
      // 选择添加部门数据
      node: {}
    }
  },

  created() {
    // 获取结构数据
    this.loadDapartment()
  },

  methods: {
    // 获取结构数据
    async loadDapartment() {
      const res = await getDapartment()
      this.companyInfo.name = res.companyName
      this.treeData = listToTree(res.depts, '')
      this.companyInfo.children = this.treeData
    },
    // 点击添加部门
    addDate(node) {
      this.node = node
      this.dialogVisible = true
    }
  }
}
</script>

<style scoped lang='scss'>
.tree-card {
  font-size:14px;
}
.el-row{
width:100%;
}
</style>
